package com.zline.foodraise.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

/**
 * Created by heshantao on 15/7/17.
 */
@EBean
public abstract class MultipleRecyclerViewBaseAdapter<T,V extends View> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String LOG_TAG = MultipleRecyclerViewBaseAdapter.class.getSimpleName();
    @RootContext
    Context cxt;
    int mHeaderCount=0;//头部View个数
    int mContentCount=0;//Content的个数
    int mBottomCount=0;//底部View个数
    SparseArray<T> headerArrys;
    SparseArray<T> contentArrys;
    SparseArray<T> bottomArrys;

    public static enum ITEM_TYPE {
        ITEM_TYPE_HEADER,
        ITEM_TYPE_CONTENT,
        ITEM_TYPE_BOTTOM
    }

    public void setHeaderViewData(SparseArray<T> sparseArray){
        this.headerArrys=sparseArray;
        mHeaderCount=headerArrys.size();
    }

    public void setContentViewData(SparseArray<T> sparseArray){
        this.contentArrys=sparseArray;
        mContentCount=contentArrys.size();
    }

    public void setBottomViewData(SparseArray<T> sparseArray){
        this.bottomArrys=sparseArray;
        mBottomCount=bottomArrys.size();
    }


    public boolean isHeaderView(int position) {
        return mHeaderCount != 0 && position < mHeaderCount;
    }

    public boolean isBottomView(int position) {
        return mBottomCount != 0 && position >= (mHeaderCount + mContentCount);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE.ITEM_TYPE_HEADER.ordinal()) {
            return onCreateHeaderView(parent, viewType);
        } else if (viewType == ITEM_TYPE.ITEM_TYPE_CONTENT.ordinal()) {
            return onCreateContentView(parent, viewType);
        } else if (viewType == ITEM_TYPE.ITEM_TYPE_BOTTOM.ordinal()) {
            return onCreateBottomView(parent, viewType);
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return  mHeaderCount + mContentCount + mBottomCount;
    }


    @Override
    public int getItemViewType(int position) {
        if (mHeaderCount != 0 && position < mHeaderCount) {//头部View
            return ITEM_TYPE.ITEM_TYPE_HEADER.ordinal();
        } else if (mBottomCount != 0 && position >= (mHeaderCount +mContentCount)) {//底部View
            return ITEM_TYPE.ITEM_TYPE_BOTTOM.ordinal();
        } else {
            return ITEM_TYPE.ITEM_TYPE_CONTENT.ordinal();
        }
    }

    protected abstract HeaderWrapper<V> onCreateHeaderView(ViewGroup parent, int viewType);//创建头部View

    protected abstract ContentWrapper<V> onCreateContentView(ViewGroup parent, int viewType);//创建中间View

    protected abstract BottomWrapper<V> onCreateBottomView(ViewGroup parent, int viewType);//穿件底部View


    static class HeaderWrapper<V extends View> extends RecyclerView.ViewHolder {
        private V view;

        public HeaderWrapper(V itemView) {
            super(itemView);
            view = itemView;
        }

        public V getView() {
            return view;
        }
    }


    static class ContentWrapper<V extends View> extends RecyclerView.ViewHolder {
        private V view;

        public ContentWrapper(V itemView) {
            super(itemView);
            view = itemView;
        }

        public V getView() {
            return view;
        }
    }


    static class BottomWrapper<V extends View> extends RecyclerView.ViewHolder {
        private V view;

        public BottomWrapper(V itemView) {
            super(itemView);
            view = itemView;
        }

        public V getView() {
            return view;
        }
    }
}
